package com.ychug.gateway.plugin;

import com.ychug.common.entity.VirtualFile;
import com.ychug.file.FileUploader;
import com.ychug.framework.plugin.file.GlobalFileUploader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传 操作处理
 *
 * @author dds
 */
@Controller
@RequestMapping("tool/upload")
public class UploadController {

    private String prefix = "modules/plugin/file";


    @GetMapping()
    public String uploadFile()
    {
        return prefix + "/viewUploadFile";
    }


    /**
     * 新增内容
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/addUploadFile";
    }


    @PostMapping("/uploadFileForMd")
    public Map<String,Object> uploadFileForMd(HttpServletRequest request, HttpServletResponse response) {



        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获得文件：
        MultipartFile file= multipartRequest.getFile("file");


        FileUploader uploader = new GlobalFileUploader();
        VirtualFile virtualFile = uploader.upload(file, "local", false);
        Map<String, Object> resultMap = new HashMap<>(3);
        resultMap.put("success", 1);
        resultMap.put("message", "上传成功");
        resultMap.put("filename", virtualFile.getFullFilePath());
        return resultMap;
    }


}
